﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using TTBusiness.DB_Linq;
using TTBusiness;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSProductAttribute
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblProduct_Attribute> getAll()
        {
            try
            {
                var list = from a in objDB.tblProduct_Attributes
                           select a;
                return list.ToList<tblProduct_Attribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct_Attribute> getByProduct(int productid)
        {
            try
            {
                var list = from a in objDB.tblProduct_Attributes
                           where a.ProductID.Equals(productid)
                           select a;
                return list.ToList<tblProduct_Attribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public tblProduct_Attribute getById(int id)
        {
            try
            {
                var obj = objDB.tblProduct_Attributes.SingleOrDefault(a => a.ID == id);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblProduct_Attribute getByProductAndCA(int productId,int cateattributeId)
        {
            try
            {
                var obj = objDB.tblProduct_Attributes.SingleOrDefault(a => a.ProductID == productId 
                    && a.CateAttributeID==cateattributeId);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblProduct_Attribute getByProductAndAttribute(int productId, int attributeId)
        {
            try
            {
                var obj = objDB.tblProduct_Attributes.SingleOrDefault(a => a.ProductID == productId
                    && a.tblCate_Attribute.AttributeID == attributeId);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }


        public void insert(tblProduct_Attribute obj)
        {
            try
            {
                objDB.tblProduct_Attributes.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblProduct_Attribute obj)
        {
            try
            {
                var objj = objDB.tblProduct_Attributes.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.ProductID = obj.ProductID;
                    objj.CateAttributeID = obj.CateAttributeID;
                    objj.Value = obj.Value;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblProduct_Attributes
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblProduct_Attributes.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
